#pragma once

#include"SpendType.h"

namespace Macro
{
	class Tickable
	{
	public:
		Tickable(Unit* realUnit)
			:_amountOfLarvas(realUnit->getLarva().size()),
			_remainMorphFrame(realUnit->getRemainingBuildTime()),
			_nowProductingSpendtype(Undefined)
		{
			_productionFinishCountDown = realUnit->getRemainingTrainTime();
			if(realUnit->isTraining())
			{
				//training queue
			}
			if(realUnit->isResearching())
			{
				_productionFinishCountDown = realUnit->getRemainingResearchTime();
				_nowProductingSpendtype = GetSpendType(realUnit->getTech());
			}
		}
		Tickable(const SpendType& spendType , const int& FrameNeeded)
			:_amountOfLarvas(0),
			_productionFinishCountDown(-1),
			_remainMorphFrame(FrameNeeded),
			_nowProductingSpendtype(Undefined)
		{
		}
		inline void Tick(bool IsLairOrHive)
		{
			if(_remainMorphFrame >= 0)
			{
				--_remainMorphFrame;
			}

			if(IsCompleted() || IsLairOrHive)
			{
				if(_productionFinishCountDown >= 0)
				{
					--_productionFinishCountDown;
				}

				if(_productionFinishCountDown < 0)
				{
					if(_amountOfLarvas < 3)
					{
						++_amountOfLarvas;
					}
					_productionFinishCountDown = 337;
				}
			}
		}
		inline bool IsCompleted() const
		{
			return _remainMorphFrame <= 0;
		}
		inline bool HaveLarva() const
		{
			return _amountOfLarvas > 0;
		}
		inline void RemoveOneLarva()
		{
			if(_amountOfLarvas > 0)
			{
				--_amountOfLarvas;
			}
		}
		inline bool MorphJustFinished() const
		{
			return _remainMorphFrame == 0;
		}
		inline bool ProductionJustFinished() const
		{
			return _productionFinishCountDown == 0;
		}
		inline bool CanProduceNow() const
		{
			return _productionFinishCountDown < 0;
		}
		inline const SpendType& ProduceWhat() const
		{
			return _nowProductingSpendtype;
		}
		inline void StartProduce(const SpendType& spendType)
		{
			_nowProductingSpendtype = spendType;
			_productionFinishCountDown = FrameNeeded(spendType);
		}
	private:
		int _amountOfLarvas;
		int _productionFinishCountDown;
		int _remainMorphFrame;
		SpendType _nowProductingSpendtype;
	};
}